<?php
class Backend_Router{

	 static protected $instance = null;
	 protected function __construct(){
	 	$this->_request = Request::getInstance();
	 }
	 protected function __clone(){}
	 
	 protected $_controller = 'Backend_Index_Controller';
	 protected $_action = 'indexAction';
	 /**
	  * @var Request
	  */
	 protected $_request;
	 /**
	  * @return Backend_Router
	  */
	 static public function getInstance(){
	 	if(is_null(self::$instance))
	 		self::$instance = new self;

	 	return self::$instance;	
	 }
	  
	 /**
	  * Route request to the Controller
	  * @return void
	  */
	 public function route()
	 {	 	
	 	$controller = $this->_request->getPath(1);
	 	$controller = ucfirst(Filter::filterValue('pagecode', $controller));

	 	if($controller!==false && strlen($controller) && class_exists('Backend_' . $controller . '_Controller'))
	 		$this->_controller = 'Backend_' . $controller . '_Controller';
	 		
	 	$controller = new $this->_controller;
	 	/**
	 	 * Easy PHP 5.2 compatibility
	 	 */
	 	//$controller->setCalledClass($this->_controller);
	 	//$controller->checkAuth();
	 		
	 	$action = strtolower($this->_request->getPath(2));
	 	
	 	if($action!==false && method_exists($controller, $action . 'Action'))
	 		$this->_action = $action . 'Action';

	 	$controller->{$this->_action}();	
	 }
}